let express = require('express');
let router = express.Router();

let DepartmentService = require('../services/DepartmentService');

router.get('/', (req, res, next) => {
    res.send({
        code: 200,
        data: null,
        msg: '部门接口请求成功'
    })
});

// 部门列表
router.get('/list', async ctx => {
    let data = await DepartmentService.getPartList();
    ctx.res.send($utils._formmat_send_data({data}));
});

// 创建
router.post('/create', async ctx => {
    let param = ctx.body;

    let data = {
        code: 100,
        data: null,
        msg: ''
    }
    if(!param.name) {
        data.msg = '请输入部门名称';
        ctx.res.send(data);
        return;
    }

    let part = await DepartmentService.createPart(param);
    console.log('after-insert-part: ' + part);
    
    ctx.res.send($utils._formmat_send_data({data:　part, changeCount: true}));
});

module.exports = router;